package com.zhangyu.shadow.ec.main.cart;

import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.joanzapata.iconify.widget.IconTextView;
import com.tepth.shadow.ui.recycler.MultipleField;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.tepth.shadow.ui.recycler.MultipleRecyclerAdapter;
import com.tepth.shadow.ui.recycler.MultipleViewHolder;
import com.zhangyu.shadow.ec.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyu
 * @date 2017/11/15
 */

class ShopCartAdapter extends MultipleRecyclerAdapter {
    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate();
    private final int mColorSelect;
    private final int mColorNormal;
    private final int mAllDataSize;
    private ShopItemSelectFlag mItemSelectFlag = ShopItemSelectFlag.INIT;
    private final ArrayList<MultipleItemEntity> mSelectList = new ArrayList<>();
    private final OnItemSelectChangeListener mChangeListener;

    ShopCartAdapter(List<MultipleItemEntity> data,
                    int colorSelect,
                    int colorNormal,
                    OnItemSelectChangeListener changeListener) {
        super(data);
        mColorSelect = colorSelect;
        mColorNormal = colorNormal;
        mChangeListener = changeListener;
        mAllDataSize = data.size();
        addItemType(ShopCartItemType.SHOP_CART_ITEM, R.layout.item_shop_cart);
    }

    final void setItemSelectFlag(ShopItemSelectFlag itemSelectFlag) {
        mItemSelectFlag = itemSelectFlag;
    }

    final ArrayList<MultipleItemEntity> getSelectList() {
        return mSelectList;
    }

    final int getSelectDataSize() {
        return mSelectList.size();
    }

    final int getAllDataSize() {
        return mAllDataSize;
    }

    @Override
    protected void convert(final MultipleViewHolder helper, final MultipleItemEntity item) {
        super.convert(helper, item);
        switch (item.getItemType()) {
            case ShopCartItemType.SHOP_CART_ITEM:
                //数据
                final String thumb = item.getField(MultipleField.IMAGE_URL);
                final String title = item.getField(ShopCartItemFields.TITLE);
                final String desc = item.getField(ShopCartItemFields.DESC);
                final int id = item.getField(MultipleField.ID);
                final double price = item.getField(ShopCartItemFields.PRICE);
                final int count = item.getField(ShopCartItemFields.COUNT);
                //控件
                final AppCompatImageView imageView = helper.getView(R.id.iv_item_goods);
                final IconTextView iconMinus = helper.getView(R.id.tv_item_goods_minus);
                final TextView tvCount = helper.getView(R.id.tv_goods_count);
                final IconTextView iconSelect = helper.getView(R.id.icon_item_shop_cart);
                final IconTextView iconAdd = helper.getView(R.id.tv_item_goods_add);
                iconMinus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int countNumber = item.getField(ShopCartItemFields.COUNT);
                        if (countNumber > 1) {
                            countNumber--;
                            item.setField(ShopCartItemFields.COUNT, countNumber);
                            tvCount.setText(String.valueOf(countNumber));
                            if (mChangeListener != null) {
                                mChangeListener.onChange();
                            }
                            //TODO 提交到服务器
                        }

                    }
                });
                iconAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int countNumber = item.getField(ShopCartItemFields.COUNT);
                        countNumber++;
                        item.setField(ShopCartItemFields.COUNT, countNumber);
                        tvCount.setText(String.valueOf(countNumber));
                        boolean isSelect = item.getField(ShopCartItemFields.IS_SELECT);
                        if (!isSelect) {
                            item.setField(ShopCartItemFields.IS_SELECT, true);
                            selectItem(false, item);
                            iconSelect.setTextColor(mColorSelect);
                        }
                        if (mChangeListener != null) {
                            mChangeListener.onChange();
                        }
                        //TODO 提交到服务器
                    }
                });

                tvCount.setText(String.valueOf(item.getField(ShopCartItemFields.COUNT)));
                Glide.with(mContext)
                        .load(thumb)
                        .apply(OPTIONS)
                        .into(imageView);
                helper.setText(R.id.tv_goods_title, title);
                helper.setText(R.id.tv_goods_detail, desc);
                helper.setText(R.id.tv_goods_price, String.valueOf(price));
                helper.setText(R.id.tv_goods_count, String.valueOf(count));
                //设置选中控件状态
                if (mItemSelectFlag == ShopItemSelectFlag.SELECT_ALL) {
                    item.setField(ShopCartItemFields.IS_SELECT, true);
                } else if (mItemSelectFlag == ShopItemSelectFlag.SELECT_NONE) {
                    item.setField(ShopCartItemFields.IS_SELECT, false);
                }
                final boolean isSelect = item.getField(ShopCartItemFields.IS_SELECT);
                iconSelect.setTextColor(isSelect ? mColorSelect : mColorNormal);
                //设置点击事件
                iconSelect.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemSelectFlag = ShopItemSelectFlag.INIT;
                        boolean isItemSelect = item.getField(ShopCartItemFields.IS_SELECT);
                        selectItem(isItemSelect, item);
                        item.setField(ShopCartItemFields.IS_SELECT, !isItemSelect);
                        iconSelect.setTextColor(!isItemSelect ? mColorSelect : mColorNormal);
                        if (mChangeListener != null) {
                            mChangeListener.onChange();
                        }
                    }
                });
                break;
            default:

                break;
        }

    }

    /**
     * 清空购物车
     */
    void clearAllItems() {
        List<MultipleItemEntity> dataList = getData();
        if (!dataList.isEmpty()) {
            mSelectList.clear();
            dataList.clear();
            if (mChangeListener != null) {
                mChangeListener.onChange();
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 删除购物车选择的商品
     */
    void deleteSelectItems() {
        if (!mSelectList.isEmpty()) {
            List<MultipleItemEntity> dataList = getData();
            for (MultipleItemEntity entity : mSelectList) {
                int position = dataList.indexOf(entity);
                dataList.remove(position);
                notifyItemRemoved(position);
            }
            mSelectList.clear();
            if (mChangeListener != null) {
                mChangeListener.onChange();
            }
        }
    }

    /**
     * 从所有数据中过滤出选中的商品
     */
    void filterSelectDataFromAllData() {
        mSelectList.clear();
        List<MultipleItemEntity> list = getData();
        if (!list.isEmpty()) {
            for (MultipleItemEntity entity : list) {
                if (entity.getField(ShopCartItemFields.IS_SELECT)) {
                    selectItem(false, entity);
                }
            }
        }
        if (mChangeListener != null) {
            mChangeListener.onChange();
        }
    }

    /**
     * 选中商品
     *
     * @param isItemSelect 是否选中
     * @param item         选择的商品
     */
    private void selectItem(boolean isItemSelect, MultipleItemEntity item) {
        if (!isItemSelect) {
            mSelectList.add(item);
        } else {
            mSelectList.remove(item);
        }

    }

    /**
     * 全选外部调用
     */
    final void selectAllItem() {
        mSelectList.clear();
        mSelectList.addAll(getData());
        for (MultipleItemEntity entity : mSelectList) {
            entity.setField(ShopCartItemFields.IS_SELECT, true);
        }
        if (mChangeListener != null) {
            mChangeListener.onChange();
        }
    }

    /**
     * 全部取消选择
     */
    final void unSelectAllItem() {
        mSelectList.clear();
        List<MultipleItemEntity> list = getData();
        for (MultipleItemEntity entity : list) {
            entity.setField(ShopCartItemFields.IS_SELECT, false);
        }
        if (mChangeListener != null) {
            mChangeListener.onChange();
        }
    }

    /**
     * 获取当前选中商品总价
     *
     * @return 选中的商品总价
     */
    final double getCurrentTotalMoney() {
        double money = 0.00;
        for (MultipleItemEntity entity : mSelectList) {
            double price = entity.getField(ShopCartItemFields.PRICE);
            int count = entity.getField(ShopCartItemFields.COUNT);
            money += price * count;
        }
        return money;
    }

    interface OnItemSelectChangeListener {
        void onChange();
    }

}
